#include<stdio.h>
#include<string.h>
#define INF 2000000000
#define M 2100000
#define N 500100
#define min(x,y) ((x)<(y)?(x):(y))
int S, T;
typedef struct {int u, v, n, q; } ADJ;
ADJ adj[M];
int num = 1;
int node[N];
int h[N], c[N];
int o[N];
int n, m;
int lim;
void addadj(int u, int v, int q)
{
	adj[num].u = u;
	adj[num].v = v;
	adj[num].q = q;
	adj[num].n = node[u];
	node[u] = num ++;
}
void add(int u, int v, int q)
{
	addadj(u, v, q);
	addadj(v, u, 0);
}
int dfs(int u, int l)
{
	int s = l, i, t, d = lim;
	if(u == T || l == 0) return l;
	for(i=o[u]; i; i=adj[i].n)
	{
		if(adj[i].q > 0)
		{
			if(h[u] == h[adj[i].v]+1)
			{
				t = min(adj[i].q, s);
				t = dfs(adj[i].v, t);
				adj[i].q -= t;
				if(i&1) adj[i+1].q += t; else adj[i-1].q += t;
				s -= t;
				if(h[S]>=lim) return l-s;
				if(s == 0) break;
			}
			if(adj[i].q > 0) d = min(d, h[adj[i].v]); //here
		}
	}
	if(i) o[u] = i; else
	{
		for(i=node[u]; i!=o[u]; i=adj[i].n)
			if(adj[i].q > 0) d = min(d, h[adj[i].v]);
		c[h[u]] --;
		if(c[h[u]] == 0) h[S] = lim;
		h[u] = d+1;
		c[h[u]] ++;
		o[u] = node[u];
	}
	return l-s;
}
int ans = 0;

int qu[N];
int vs[N];
int g[N];
int p, q;
int li[M];

void bfs()
{
	int i, t, pt = 0;
	p = 0; q = 1;
	qu[0] = S;
	vs[S] = 1;
	g[S] = INF;
	while(p < q)
	{
		for(i=node[qu[p]]; i; i=adj[i].n)
		{
			if(g[qu[p]] == 0) break;
			if(i & 1) continue;
			li[pt ++] = i^1;
			t = min(adj[i].q, g[qu[p]]);
			adj[i].q -= t;
			adj[i^1].q += t;
			g[qu[p]] -= t;
			g[adj[i].v] += t;
			if(!vs[adj[i].v])
			{
				vs[adj[i].v] = 1;
				qu[q ++] = adj[i].v;
			}
		}
		p ++;
	}
	g[T] = 0;
	for(i=pt-1; i>=0; i--)
	{
		p = adj[li[i]].u;
		q = adj[li[i]].v;
		t = min(adj[li[i]].q, g[p]);
		adj[li[i]].q -= t;
		adj[li[i]^1].q += t;
		g[p] -= t;
		g[q] += t;
	}
	memset(vs, 0, sizeof(vs));
	qu[0] = T;
	vs[T] = 1;
	p = 0; q = 1;
	while(p < q)
	{
		c[h[qu[p]]]++;
		for(i=node[qu[p]]; i; i=adj[i].n)
		{
			if(!vs[adj[i].v])
			{
				vs[adj[i].v] = 1;
				h[adj[i].v] = h[qu[p]] + 1;
				qu[q ++] = adj[i].v;
			}
		}
		p ++;
	}
}

int main(void)
{
	int i;
	int a, b, x;
	scanf("%d %d", &n, &m);
	for(i=0; i<m; i++)
	{
		scanf("%d %d %d", &a, &b, &x);
		add(a, b, x);
	}
	scanf("%d %d", &S, &T);
	lim = n + 2;
	c[0] = lim;
	bfs();
	memcpy(o, node, sizeof(node));
	while(h[S]<lim) ans += dfs(S, INF);
	printf("%d\n", ans);

	//while(1);
	return 0;
}
